// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import GameBoard from "./game/gameboard";
import Hero from "./hero";
import {MAP_CONFIG} from "./config/config";
import {ballFactory} from "./game/ballFactory";
import {globalConfig} from "./config/globalConfig";
import {GameVar, GlobalObject} from "./game/GameVar";
import AiScript from "./ai-script";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Point extends cc.Component {


  private _gameBoard: GameBoard;

  @property(cc.SpriteFrame)
  bubble0: cc.SpriteFrame = null;
  @property(cc.SpriteFrame)
  bubble1: cc.SpriteFrame = null;
  @property(cc.SpriteFrame)
  bubble2: cc.SpriteFrame = null;
  @property(cc.SpriteFrame)
  bubble3: cc.SpriteFrame = null;
  @property(cc.SpriteFrame)
  bubble4: cc.SpriteFrame = null;

  // private players: Array<Hero> = [];

  private config: any = null;
  private scale = 0.3;
  private isMoving = false;

  onLoad() {
  }

  start() {
    this.node.runAction(this.getAction())
  }

  random() {
    this.randomPos();
    this.randomColor();
    this.randomConfig();
  }

  private randomColor() {
    this.getComponent(cc.Sprite).spriteFrame = this['bubble' + Math.floor(Math.random() * 5)]
  }

  private randomPos() {
    this.node.x = MAP_CONFIG.startx + Math.floor(MAP_CONFIG.width * Math.random())
    this.node.y = MAP_CONFIG.starty + Math.floor(MAP_CONFIG.height * Math.random())
  }

  private randomConfig() {
    this.config = globalConfig.sceneitem_config[Math.floor(Math.random() * globalConfig.sceneitem_config.length)];
    this.scale = this.config.ballscale / 100;
    this.node.scale = this.scale;
  }

  set gameBoard(value: GameBoard) {
    this._gameBoard = value;
  }

  onCollisionEnter(other: cc.BoxCollider, self) {
    if (!this.enabled || this.isMoving) {
      return;
    }
    this.enabled = false;
    const action = cc.moveTo(0.1, other.node.x, other.node.y);
    const hero = other.getComponent(Hero)
    const aiScript = other.getComponent(AiScript)
    const seq = cc.sequence(action, cc.callFunc(() => {
      ballFactory.recycle(this.node)
      hero.addEnergy(this.config.ballenergy);
      if (hero === GameVar.myHero) {
        hero.addScore(this.config.ballscore)
        GlobalObject.voiceSource.playBubble();
      } else if (aiScript) {
        const score = aiScript.getScoreRatio() * this.config.ballscore;
        hero.addScore(score)
      }
    }, this))
    this.node.runAction(seq)
  }

  private getAction() {
    return cc.sequence(cc.delayTime(Math.random() * 2), cc.scaleTo(1, this.scale * 0.9, this.scale * 0.9).easing(cc.easeSineInOut()), cc.scaleTo(1, this.scale, this.scale).easing(cc.easeSineInOut())).repeatForever();
  }

  launch() {
    const x = Math.random() * 200 - 100;
    const y = Math.random() * 200 - 100;
    let newX = x + this.node.x;
    let newY = y + this.node.y;
    if (newX < MAP_CONFIG.startx) {
      newX = MAP_CONFIG.startx;
    } else if (newX > MAP_CONFIG.startx + MAP_CONFIG.width) {
      newX = MAP_CONFIG.startx + MAP_CONFIG.width
    }
    if (newY < MAP_CONFIG.starty) {
      newY = MAP_CONFIG.starty;
    }
    else if (newY > MAP_CONFIG.starty + MAP_CONFIG.height) {
      newY = MAP_CONFIG.starty + MAP_CONFIG.height
    }
    this.isMoving = true;
    const action = cc.sequence([cc.moveTo(0.5, newX, newY).easing(cc.easeOut(2)), cc.callFunc(() => {
      this.isMoving = false;
    })]);
    this.node.runAction(action);
  }
}
